﻿<%@ Page Title="部门管理" Language="C#" MasterPageFile="~/master/m.master" %>
<script runat="server"></script>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
	<link href="../assets/css/plugins/jsTree/style.min.css" rel="stylesheet">
	<link href="../assets/css/plugins/footable/footable.core.css" rel="stylesheet">
	<link href="../assets/css/plugins/iCheck/custom.css" rel="stylesheet">
	<link href="../assets/img/validate/validate.css" rel="stylesheet">
	<style type="text/css">
		@media (min-width: 768px) {
			.modal-dialog {
				width: 768px;
				margin: 30px auto;
			}
		}
		
		.tableshow a {
			color: green;
		}
		
		.right-side1 {
			height: 100%;
			float: left;
			overflow: auto;
		}
		
		.footable1 {
			width: 100%;
		}
		
		.tdpadding {
			padding: 0;
		}
		
		.page-tips {
			background: #fffdcd;
			color: #bc7b21;
			font-size: 13px;
			padding: 4px 0 0 5px;
			clear: both;
		}
	</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="main" Runat="Server">
	<div class="system-box">
		<div class="btns-container">

		</div>
		<div class="left-side  ibox-content">
			<div id="using_json"></div>
		</div>
		<div class="right-side">
			<!--rl-->
			<div class="col-sm-8 col-xs-8 col-md-8 col-lg-8" style="padding: 0;">
				<!--title-->
				<div class="row show-grid" style="margin: 0;">
					<div class="col-xs-12 col-sm-12 col-md-12 col-mlg-12" style="padding-top: 5px;padding-bottom: 5px;">开发用户列表</div>
				</div>
				<!--搜索框-->
				<div class="search-container">
					<div class="input-group">
						<input type="text" class="form-control textUser" placeholder="在此输入关键字以查找用户">
						<span class="input-group-btn">
                		<button type="button" class="btn btn-primary btnsearch">
                			<i class="fa fa-search"></i>&nbsp;搜索
                		</button>
                	</span>
					</div>
				</div>
				<!--列表-->
				<div class="right-content-box">
					<div class="ibox-content">
						<table class="footable table table-stripped table-list">
							<thead></thead>
							<tbody id="pageList"></tbody>
						</table>
					</div>
				</div>
				<!--r1-->
			</div>
			<!--rr-->
			<div div class="col-sm-4 col-xs-4 col-md-4 col-lg-4" style="padding: 0;">
				<!--title-->
				<div class="row show-grid" style="margin: 0;">
					<div class="col-xs-12 col-sm-12 col-md-12 col-mlg-12" style="padding-top: 5px;padding-bottom: 5px;">开发用户列表</div>
				</div>
				<!--搜索框-->
				<div class="search-container">
					<div class="page-tips" style="padding: 8px 0;">
						<i class="fa fa-lightbulb-o"></i>
						<span>注：所有用户权重分值范围是1-10之间.</span>
					</div>
				</div>
				<!--列表-->
				<div class="right-content-box">
					<div class="ibox-content">
						<table class="footable1 table table-stripped table-list">
							<thead></thead>
							<tbody id="pageList1"></tbody>
						</table>
					</div>
				</div>
				<!--r1-->
			</div>
		</div>

		<!--<input type="number"  style="width:20%;height:15px;" onchange="mustN(this)" step="0.1"  value=""/>-->
		<!---->
	</div>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="footer" Runat="Server">
	<script src="../assets/js/plugins/jsTree/jstree.min.js"></script>
	<script src="../assets/js/plugins/footable/footable.all.min.js"></script>
	<script src="../assets/js/plugins/iCheck/icheck.min.js"></script>
	<script src="../assets/js/page/dept.js"></script>
	<script src="../assets/js/page/rScroll.js"></script>
	<script src="../assets/js/page/tableControl.js"></script>
	<script src="../assets/img/validate/learunui-validator.js"></script>
	<script type="application/javascript">
		
		$(document).ready(function(tBody2) {
		    console.log(tBody2);
		    console.log(typeof(tBody2))
			//初始化多选框
			$(".i-checks").iCheck({
				checkboxClass: "icheckbox_square-green",
			});

			//用于记录当前选中节点id（部门id）
			var deptId = 1;
			//用于记录当前选中节点的代码
			var deptCode = '1';
			//用于记录父节点的代码
			var parentCode = '';
			//用于记录当前选中节点grade
			var deptGrade = 1;
			//用于给予当前选中节点父id
			var deptParent = 0;
			//用于标记是新增还是编辑
			var isCreate = true;
			//用于记录当前节点的名称
			var deptContent = '';
			//用于记录所属部门
			var belong = '';
			//模块ID
			var ModuleID = Y.getUrlParam("ModuleID");
			//初始树形列表
			var initTree = true;

			//加载导航列表
			var loadList = function(init) {
				//调用加载层

				layer.msg('玩命加载中……', {
					icon: 16,
					shade: 0.5,
					time: 0,
					maxWidth: 200
				});
				initTree = true;
				Y.API2('Dept', 'GetList', {
					DeptCode: 1
				}, function(data) {
					//关闭加载层
					layer.closeAll('dialog');
					var treeData = {};
					if(!Y.checkNull(data) && data.length > 0) {
						//json处理
						data.map(function(item, i) {
							treeData.text = item.DeptContent;
							treeData.data = {
								Grade: item.Grade,
								DeptID: item.DeptID,
								DeptCode: item.DeptCode,
								ParentID: item.ParentID,
							};
							if(item.Children.length > 0) {
								treeData.state = {
									opened: true
								};
								treeData.children = deptUtils.getChildNode(item.Children);
							}
						})

						//初始化左侧菜单
						$.jstree.destroy("#using_json");
						$('#using_json').jstree({
							core: {
								data: [treeData]
							}
						}).bind('select_node.jstree', function(event, data) {
							initTree = false;
							//绑定的点击事件
							var node_data = data.node.data;
							deptId = node_data.DeptID;
							deptCode = node_data.DeptCode;
							deptGrade = node_data.Grade;
							deptParent = node_data.ParentID;
							belong = data.node.text;

							if(deptId == 1) {
								$('#editBtn').attr('disabled', 'disabled');
								$('#deleteBtn').attr('disabled', 'disabled');
							} else {
								$('#editBtn').removeAttr('disabled');
								$('#deleteBtn').removeAttr('disabled');
							}

							//加载右侧列表
							loadPageList();
						});

						//判断是否为初次调用
						if(!Y.checkNull(init)) {
							//获取初始化的相关属性
							deptId = data[0]['DeptID'];
							deptGrade = data[0]['Grade'];
							deptParent = data[0]['ParentID'];
							belong = data[0]['DeptContent'];
							deptCode = data[0]['DeptCode'];
							//加载右侧列表
							loadPageList();

							//禁用编辑和删除
							$('#editBtn').attr('disabled', 'disabled');
							$('#deleteBtn').attr('disabled', 'disabled');
						}
					}
				});
			}
			loadList(true);

			//获取右侧内容列表
			var loadPageList = function() {
				if($('.footable thead').children().size() == 0)
					tableControl.initThead2('.footable', ['用户名称', '用户编码', '所属部门', '所在岗位', '角色名称', '选择']);

				Y.API2('Users', 'GetList', {
					DeptID: deptId,
				}, function(data) {
					if(data.Success == false) {
						swal(data.Message, '', 'error');
					} else {
						var tBody = [];

						//
						if(!Y.checkNull(data) && data.Items.length > 0) {
							data.Items.map(function(item, i) {
								item.belong = belong;
							});

							data.Items.map(function(item, i) {
								var UserID = item.UserID;
								var tempTr = {
									trClass: (i % 2 == 0 ? 'gradeX footable-even' : 'gradeX footable-odd'),
									children: [{
											tdText: item.UserName,
											c: 0,
											r: 0,
											tdHtml: '',
											tdId: '',
											tdClass: ''
										},
										{
											tdText: item.UserNo,
											c: 0,
											r: 0,
											tdHtml: '',
											tdId: '',
											tdClass: ''
										},
										{
											tdText: item.UserNo,
											c: 0,
											r: 0,
											tdHtml: '',
											tdId: '',
											tdClass: 'hide'
										},
										{
											tdText: (item.DeptName || ' '),
											c: 0,
											r: 0,
											tdHtml: '',
											tdId: '',
											tdClass: ''
										},
										{
											tdText: (item.PostName || ' '),
											c: 0,
											r: 0,
											tdHtml: '',
											tdId: '',
											tdClass: ''
										},
										{
											tdText: (item.RoleName || ' '),
											c: 0,
											r: 0,
											tdHtml: '',
											tdId: '',
											tdClass: ''
										},
										{
											tdText: ' ',
											c: 0,
											r: 0,
											tdHtml: '<a href="#" userid=' + UserID + ' onclick="msgChoice(this)">选中</a>',
											tdId: '',
											tdClass: 'tableshow'
										},

									]
								};

								tBody.push(tempTr);
							})
						}
						tableControl.initTbody('.footable', (tBody.length > 0 ? tBody : null), 5, null);
					}
				});
			}
			//右列表2加载
			var loadPageList2 = function() {
				if($('.footable thead').children().size() == 0)
					tableControl.initThead2('.footable1', ['用户名称', '用户名称', '评分权重']);
				//api
				Y.API2('UserWeight', 'GetUserWeightList', {}, function(data) {
					console.log('GetUserWeightList')
					console.log(data)
					if(data.Success == false) {
						swal(data.Message, '', 'error');
					} else {
						if(data == null) {
							var tBody2 = [{
								trClass: ('gradeX footable-odd'),
								children: [{
										tdText: '  ',
										c: 0,
										r: 0,
										tdHtml: '',
										tdId: '',
										tdClass: ''
									},
									{
										tdText: '  ',
										c: 0,
										r: 0,
										tdHtml: '',
										tdId: '',
										tdClass: ''
									},
									{
										tdText: '  ',
										c: 0,
										r: 0,
										tdHtml: '',
										tdId: '',
										tdClass: ''
									},
								]
							}];
							
							tableControl.initTbody('.footable1', (tBody2.length > 0 ? tBody2 : null), 5, null);
						}else{
						   console.log('data-length>0')
						   var dataLists=[];
						   $.each(data,function(i,item){
						       var ListData = {
								trClass: ('gradeX footable-odd'),
								children: [{
										tdText: '  ',
										c: 0,
										r: 0,
										tdHtml: '',
										tdId: item.UserID,
										tdClass: ''
									},
									{
										tdText: (item.UserName||' '),
										c: 0,
										r: 0,
										tdHtml: '',
										tdId: '',
										tdClass: ''
									},
									{
										tdText: '  ',
										c: 0,
										r: 0,
										tdHtml: '<input type="number"  style="width:20%;height:15px;" onchange="mustN(this)" step="0.1" value='+item.Weight+' />',
										tdId: item.UserID,
										tdClass: ''
									},
								]
							};
							dataLists.push(ListData)
						   })
						   console.log(dataLists)
						   tableControl.initTbody('.footable1', (dataLists.length > 0 ? dataLists : null), 5, null);
						//这里绑定数据
						}
					}
				})
				//

				tableControl.initTbody('.footable1', (tBody2.length > 0 ? tBody2 : null), 5, null);
			}
//			loadPageList2()

			var loadIndex = function() {
				Y.API2('Common', 'GetSort', {
					"tableName": "sys_dept",
					"sortName": "sort"
				}, function(data) {
					if(data.Success == false) {
						//layer.msg(data.Message);
					} else {
						$("#sort").val(data + 1);
						$("#sort").attr("readonly", true);
					}
				});
			}
			//获取按钮
			commonUtil.loadBtnList(ModuleID, function() {
				rScroll()
			});

			//刷新按钮
			$('.btns-container').on('click', '#refreshBtn', function() {
				loadList(true);
				//	            loadPageList();
			});
			//新增按钮
			$('.btns-container').on('click', '#addBtn', function() {
				if(initTree) {
					swal('请先在目录树选择节点');
					return false;
				}
				$("#companyID").parent().addClass("hide")
				$("#companyID").removeClass("required");
				if(deptId == 1) {
					$("#companyID").parent().removeClass("hide")
					$("#companyID").addClass("required");
				}
				$('.editTitle').text('新增部门');
				isCreate = true;
				$(':input', '#checkModuleForm').not(':button, :submit, :reset').val('');
				$('#kind').val(1);
				$('#restsetid').val(1);
				$('#deptName').val(belong); //获取当前节点名称
				loadIndex();
			});
			//数据联动
			$("#deptContent").keyup(function() {
				var beginStr = $("#deptName").val().split("/")[0];
				var dept = $("#deptContent").val();
				$("#deptName").val(beginStr + (dept == "" ? "" : "/" + dept));
			});

			//编辑按钮ParentCode
			$('.btns-container').on('click', '#editBtn', function() {
				$("#companyID").parent().addClass("hide")
				$("#companyID").removeClass("required");
				if(deptParent == 1) {
					$("#companyID").parent().removeClass("hide")
					$("#companyID").addClass("required");
				}
				$('.editTitle').text('编辑部门');
				isCreate = false;
				layer.msg('玩命加载中……', {
					icon: 16,
					shade: 0.5,
					time: 0,
					maxWidth: 200
				});
				Y.API2('Dept', 'GetID', {
					DeptID: deptId,
				}, function(data) {
					layer.closeAll('dialog');
					if(data.Success == false) {
						swal(data.Message, '', 'error');
					} else {
						$('#companyID').val(data.CompanyID);
						$('#deptName').val(data.DeptName);
						$('#deptContent').val(data.DeptContent);
						$('#deptCode').val(data.DeptCode);
						$('#kind').val(data.Kind);
						$('#costCode').val(data.CostCode);
						$('#restsetid').val(data.Restsetid);
						$('#sort').val(data.Sort);
						$('#storeCode').val(data.StoreCode);
						$('#storeAddr').val(data.StoreAddr);
						parentCode = data.ParentCode;
					}
				})
			});

			//点击删除
			commonUtil.initDeleteBtnEvent({
				callBack: function() {
					Y.API2('Dept', 'Remove', {
						DeptID: deptId
					}, function(data) {
						layer.closeAll('dialog');
						if(data.Success == false) {
							swal('删除失败', data.Message, 'error');
						} else {
							swal('删除成功', '该信息已被你永久删除', 'success');
							$('#refreshBtn').click();
						}
					});
				}
			});

			//检验提交信息
			$('#addModuleBtn').click(function() {
				var CompanyID = $('#companyID').val();
				if(!CheckDataValid('#checkModuleForm')) {
					return false;
				}
				if(deptId == 1 && Y.checkNull(CompanyID)) {
					tipCss($('#companyID'), "公司代码不能为空！");
					return false;
				}

				//检验通过后执行新增或者编辑
				var deal = '';
				var parentid = 0;
				var successText = '';
				if(isCreate) {
					deal = 'Create';
					successText = '新增成功';
				} else {
					deal = 'Modify';
					successText = '编辑成功';
				}

				layer.msg('玩命加载中……', {
					icon: 16,
					shade: 0.5,
					time: 0,
					maxWidth: 200
				});
				Y.API2('Dept', deal, {
					DeptID: deptId,
					CompanyID: CompanyID,
					DeptCode: $('#deptCode').val(),
					DeptContent: $('#deptContent').val(),
					Kind: $('#kind').val(),
					DeptName: $('#deptName').val(),
					CostCode: $('#costCode').val(),
					Restsetid: $('#restsetid').val(),
					StoreCode: $('#storeCode').val(),
					StoreAddr: $('#storeAddr').val(),
					ParentID: isCreate ? deptId : deptParent,
					ParentCode: isCreate ? deptCode : parentCode,
					Grade: isCreate ? (deptGrade + 1) : deptGrade,
					Sort: $('#sort').val()
				}, function(data) {
					layer.closeAll('dialog');
					if(data.Success == false) {
						swal(data.Message, '', 'error');
					} else {
						swal(successText, '', 'success');
						//关闭新建  or 编辑框
						$('#closeAddBox').click();
						//刷新列表
						$('#refreshBtn').click();
					}
				})

			})

			$('.btns-container').on('click', '#btn-save', function() {
				var submitData = $('#pageList1 tr');
				var btnData = [];
				$.each(submitData, function(i, item) {
					var pushData = {
						UserID: $(this, 'td')[0].cells[2].id,
						Weight: $(this, 'td').children().filter('.tdpadding1').children().val()
					}
					btnData.push(pushData)
				});
				console.log(btnData)
				Y.API2('UserWeight', 'Save', {
					list: btnData
				}, function(data) {
					alert('保存成功')
				})
			});

		});
		
		var tBody2 = [];
		//判断输入框的内容
		function mustN(t) {
			if(t.value > 10) {
				t.value = 10;
			}
			if(t.value < 1) {
				t.value = 1;
			}
			if(t.value.length > 3) {
				t.value = t.value.slice(0, 3)
			}
			$.each(tBody2, function(i, item) {
				if(item.children[2].tdId == $(t).parent().attr('id')) {
					tBody2[i].children[2].tdHtml = '<input type="number"  style="width:20%;height:15px;" onchange="mustN(this)" step="0.1" value=' + t.value + ' />'
				}
			});
		}
		
		
		var loadPageList2 = function() {
				if($('.footable thead').children().size() == 0)
					tableControl.initThead2('.footable1', ['用户名称', '用户名称', '评分权重']);
				//api
				Y.API2('UserWeight', 'GetUserWeightList', {}, function(data) {
					console.log('GetUserWeightList')
					console.log(data)
					if(data.Success == false) {
						swal(data.Message, '', 'error');
					} else {
						if(data == null) {
							 tBody2 = [{
								trClass: ('gradeX footable-odd'),
								children: [{
										tdText: '  ',
										c: 0,
										r: 0,
										tdHtml: '',
										tdId: '',
										tdClass: ''
									},
									{
										tdText: '  ',
										c: 0,
										r: 0,
										tdHtml: '',
										tdId: '',
										tdClass: ''
									},
									{
										tdText: '  ',
										c: 0,
										r: 0,
										tdHtml: '',
										tdId: '',
										tdClass: ''
									},
								]
							}];
							
							tableControl.initTbody('.footable1', (tBody2.length > 0 ? tBody2 : null), 5, null);
						}else{
						   $.each(data,function(i,item){
						       var ListData = {
								trClass: ('gradeX footable-odd'),
								children: [{
										tdText: '  ',
										c: 0,
										r: 0,
										tdHtml: '<a href="#" onclick="msgRemove(this)">移除</a>',
										tdId: item.UserID,
										tdClass: ''
									},
									{
										tdText: (item.UserName||' '),
										c: 0,
										r: 0,
										tdHtml: '',
										tdId: '',
										tdClass: ''
									},
									{
										tdText: '  ',
										c: 0,
										r: 0,
										tdHtml: '<input type="number"  style="width:20%;height:15px;" onchange="mustN(this)" step="0.1" value='+item.Weight+' />',
										tdId: item.UserID,
										tdClass: ''
									},
								]
							};
							tBody2.push(ListData)
						   })
						   console.log(tBody2)
						   tableControl.initTbody('.footable1', (tBody2.length > 0 ? tBody2 : null), 5, null);
						//这里绑定数据
						}
					}
				})
				//

				tableControl.initTbody('.footable1', (tBody2.length > 0 ? tBody2 : null), 5, null);
			}
		
		loadPageList2();



		//点击移除事件
		function msgRemove(t) {
			$(t).parent().parent().remove()

		}
		//点击选中事件
		function msgChoice(t) {
			var userId = $(t).attr('userid')
			var userName = $(t).parent().siblings().eq(0).text();
			var fenshu = '<input type="text" value="" />';
			var tBodylist = {
				trClass: '',
				children: [{
						tdText: '',
						c: 0,
						r: 0,
						tdHtml: '<a href="#" onclick="msgRemove(this)">移除</a>',
						tdId: userId,
						tdClass: ''
					},
					{
						tdText: userName,
						c: 0,
						r: 0,
						tdHtml: '',
						tdId: '',
						tdClass: 'tdpadding'
					},
					{
						tdText: '',
						c: 0,
						r: 0,
						tdHtml: '<input type="number"  style="width:20%;height:15px;" onchange="mustN(this)" step="0.1"  />',
						tdId: userId,
						tdClass: 'tdpadding1'
					},
				]
			};

			var control = 0;
			$.each(tBody2, function(i, item) {
				if(item.children[0].tdId != userId || item.children[0].tdId == null) {

				} else {
					control++;
				}

			});
			if(control == 0) {
				tBodylist.trClass = ((tBody2.length + 1) % 2 == 0 ? 'gradeX footable-odd' : 'gradeX footable-even');
				tBody2.push(tBodylist)
			} else {
				console.log('userId the same');
			}

			tableControl.initTbody('.footable1', (tBody2.length > 0 ? tBody2 : null), 3, null);
		}
	</script>
</asp:Content>